﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace BondedWareHouse
{
    public partial class F_MasterMaintenance_Product : Form
    {
        public F_MasterMaintenance_Product()
        {
            InitializeComponent();
        }


        #region"Methods"

        private bool P_CheckData()
        {
            if (this.txt_Search_Shoh_CD.Text.Trim() == "")
            {
                MessageBox.Show("产品代码 不能为空 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txt_Search_Shoh_CD.Focus();
                return false;
            }
            if (this.txt_Shoh_NM.Text.Trim() == "")
            {
                MessageBox.Show("产品名称 不能为空 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txt_Shoh_NM.Focus();
                return false;
            }

            if (this.txt_Hinmei_NM.Text.Trim() == "")
            {
                MessageBox.Show("客户产品代码 不能为空 !", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.txt_Hinmei_NM.Focus();
                return false;
            }
            return true;
        }

        private void P_ClearData()
        {
            this.txt_Search_Shoh_CD.Text = "";
            this.txt_Shoh_NM.Text = "";
            this.txt_Hinmei_NM.Text = "";
            this.txt_Search_Shoh_CD.Enabled = true;
        }

        #endregion

        #region"windows events"

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            try
            {

                if (this.txt_Search_Shoh_CD.Text.Trim() == "")
                {
                    MessageBox.Show("产品代码不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.txt_Search_Shoh_CD.Focus();
                    return;
                }

                clsPublic.DB.BeginTransaction();

                string sql = "";

                sql = "Delete from M_Product_Main where Shoh_CD='" + this.txt_Search_Shoh_CD.Text.Trim() + "'";
                clsPublic.DB.ExecSQL(sql);
                clsPublic.DB.Commit();
                MessageBox.Show("删除成功 ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                P_ClearData();
                this.txt_Search_Shoh_CD.Focus();
            }
            catch (Exception ex)
            {
                clsPublic.DB.RollBack();
                MessageBox.Show("删除失败 " + ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel_bottom_Paint(object sender, PaintEventArgs e)
        {

        }

    

        private void F_MasterMaintenance_Product_Load(object sender, EventArgs e)
        {
            try
            {
                this.btn_Delete.Enabled = false;
                this.btn_Search_Shoh_CD.Focus();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {
                if (!P_CheckData())
                {
                    return;
                }
                clsPublic.DB.BeginTransaction();

                string sql = "";
                string strsql = "";
                sql = "select * FROM M_Product_Main WHERE Shoh_CD='" + this.txt_Search_Shoh_CD.Text.Trim() + "'";
                DataSet ds;
                ds = clsPublic.DB.ReadTable(sql);
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    //新增
                  
                    strsql = "Insert Into M_Product_Main(Del_FG,Tori_CD,Nins_CD,Shoh_CD,Shoh_NM,Iri_QT,Hinmei_NM,Reg_DT,Upd_DT)   ";
                    strsql =strsql+ "values(0,'FDTH','FDTH','" + this.txt_Search_Shoh_CD.Text.Trim() + "','" + this.txt_Shoh_NM.Text.Trim() + "',1,'" + this.txt_Hinmei_NM.Text.Trim() + "','" + DateTime.Now.ToShortDateString() + "','" + DateTime.Now.ToShortDateString() + "')";


                }
                else
                {
                    //修修
                    strsql = "Update M_Product_Main set Shoh_NM='" + this.txt_Shoh_NM.Text.Trim() + "' , Hinmei_NM='" + this.txt_Hinmei_NM.Text.Trim() + "' where Shoh_CD='" + this.txt_Search_Shoh_CD.Text.Trim() + "'";
                }

                clsPublic.DB.ExecSQL(strsql);
                clsPublic.DB.Commit();

                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.P_ClearData();
                this.btn_Delete.Enabled = false;
                this.txt_Search_Shoh_CD.Focus();
            }
            catch (Exception ex)
            {
                 clsPublic.DB.RollBack();
                 MessageBox.Show("数据保存失败 "+ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txt_Search_Shoh_CD_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_Search_Shoh_CD_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txt_Shoh_NM.Focus();
            }
        }

        private void txt_Shoh_NM_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_Shoh_NM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.txt_Hinmei_NM.Focus();
            }
        }

      

        private void btn_Search_Click(object sender, EventArgs e)
        {
            try
            {
                 
                string sql = "";
              
                sql = "select * FROM M_Product_Main WHERE Shoh_CD='" + this.txt_Search_Shoh_CD.Text.Trim() + "'";

                DataSet ds;
                ds = clsPublic.DB.ReadTable(sql);

                if ((ds.Tables.Count > 0) && (ds.Tables[0].Rows.Count > 0))
                {
                    this.btn_Delete.Enabled = false;

                    DataRow Row = ds.Tables[0].Rows[0];

                    this.txt_Shoh_NM.Text = Row["Shoh_NM"].ToString();
                    this.txt_Hinmei_NM.Text = Row["Hinmei_NM"].ToString();

                }
                else
                {
                    MessageBox.Show("产品代码不存在，可以新增。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.txt_Shoh_NM.Focus();
                    this.btn_Delete.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void txt_Hinmei_NM_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.btn_Save.Focus();
            }
        }

        private void btn_Search_Shoh_CD_Click(object sender, EventArgs e)
        {
            F_COM_ProductMasterReference fp = new F_COM_ProductMasterReference();

            if (fp.ShowDialog() == DialogResult.OK)
            {
                this.txt_Search_Shoh_CD.Text = fp.Shoh_CD;
                this.txt_Shoh_NM.Text = fp.Shoh_NM;
                this.txt_Hinmei_NM.Text = fp.Hinmei_NM;
                this.btn_Delete.Enabled = true;
                this.txt_Search_Shoh_CD.Enabled = false;

            }
            else
            {
                this.txt_Search_Shoh_CD.Focus();
                this.txt_Search_Shoh_CD.Enabled = true;
            }
        }

        private void btn_Clear_Click(object sender, EventArgs e)
        {
            this.P_ClearData();
            this.txt_Search_Shoh_CD.Focus();
        }
    }
}
